import { Component, OnInit, ChangeDetectorRef, ViewChild, Inject, forwardRef } from '@angular/core';
import { masterService } from '../../../../lib/master.service';
import { path } from '../../../path.config';
import { QuillEditorComponent } from '../recharge-note/quill-editor/quill-editor.component';
import { AppComponent } from '../../../app.component';

@Component({
    selector: 'app-recharge-note',
    templateUrl: './recharge-note.component.html',
    styleUrls: ['./recharge-note.component.css'],
    providers: [masterService, forwardRef(() => AppComponent)]
})

export class RechargeNoteComponent implements OnInit {
    @ViewChild('editor') private editor: QuillEditorComponent;

    constructor(private _masterService: masterService,
        private cdRef: ChangeDetectorRef,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) {
    }

    public notice = { "id": 0, "content": "" };

    ngOnInit() {
        this.get();
    }

    get(): void {
        this._masterService
            .getData(path.accountRecharge.rechargeManage.getnotice, null)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.editor.editorContent = res.data.content;
                    this.notice = res.data;
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            });
    }

    create(): void {
        this._masterService
            .postData(path.accountRecharge.rechargeManage.createnotice, { "id": this.notice.id, "content": this.editor.editorContent })
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.app.toastSuccess('保存成功！');
                    this.get();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage)
                }
            });
    }
}
